#include <stdio.h>
#include <stdlib.h>

#include "glm/glm.h"
#include "definitions.h"
#include "init.h"
#include "vector.h"
#include "lightSource.h"

#define TICKS_FOR_60_FPS 16

GLMmodel* model;

void makeViewport()
{
    glViewport(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
}


void configureLight()
{
    Vector ambient(0.3,0.3,0.3);
    LightSource::ambientLight(ambient);

}

void processEvents()
{
    SDL_Event event;

    while (SDL_PollEvent(&event) != 0)
    {
        switch (event.type)
        {
            case SDL_QUIT:
                classyExit(0);
                break;
        }
    }
}

void draw()
{

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    configureLight();

    glPushMatrix();

       glColor3f(1,0,1);
       glScalef(0.08,0.08,0);
       glmDraw(model, GLM_SMOOTH | GLM_TEXTURE);

    glPopMatrix();

    SDL_GL_SwapBuffers();
}

int main(int argc, char **argv)
{

    setupAll();

    model = glmReadOBJ("models/Trickster.obj");
    glmFacetNormals(model);
    glmVertexNormals(model, 90.0);

    unsigned int update = SDL_GetTicks(),lastDraw, frames = 0;
    float fps = 0;

    while (true)
    {
        if ( (SDL_GetTicks() - update) > 1000)
        {
            char newTitle[20];

            fps = (float) frames / (SDL_GetTicks() - update) * 1000;
            sprintf(newTitle,"Blue Game of Death %.2f\n",fps);

            SDL_WM_SetCaption(newTitle,NULL);

            frames = 0;
            update = SDL_GetTicks();
        }

        if ( (SDL_GetTicks() - lastDraw) > TICKS_FOR_60_FPS)
        {
            draw();
            processEvents();
            frames++;
            lastDraw = SDL_GetTicks();
        }
    }

    return 0;
}

